﻿using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Nodes.Operations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.OpenCode.Framework.DevForm
{
    /// <summary>
    /// 树节点检索查询类。
    /// </summary>
    public class TreeSearchOperation : TreeListOperation
    {
        private string _keyWord;
        private List<string> _fieldList;

        /// <summary>
        /// 查询结果。
        /// </summary>
        private List<TreeListNode> _resultNodes;

        /// <summary>
        /// 根据关键字查找到的节点。
        /// </summary>
        public List<TreeListNode> FindNodes
        {
            get { return _resultNodes; }
        }

        /// <summary>
        /// 构造<see cref="TreeSearchOperation"/>的实例。
        /// </summary>
        /// <param name="keyWord">查询关键字</param>
        /// <param name="fieldList">参与查询字段集合</param>
        public TreeSearchOperation(string keyWord, List<string> fieldList)
        {
            _keyWord = keyWord;
            _fieldList = fieldList;
            _resultNodes = new List<TreeListNode>();
        }

        /// <summary>
        /// 执行查询。
        /// </summary>
        public override void Execute(TreeListNode node)
        {
            if (_fieldList == null) return;
            foreach (string fieldName in _fieldList)
            {
                object value= node.GetValue(fieldName);
                if (Convert.IsDBNull(value)) continue;
                if (value == null) continue;
                string fieldValue = value.ToString();
                if (fieldValue.Contains(_keyWord))
                {
                    _resultNodes.Add(node);
                }
            }
        }
    }
}
